<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/11
 *  Q Q: 505413
 * Time: 11:59
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
class Link  extends Base
{
    //友链列表
    public function index()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['web_name'] = ['like',"%" . $key . "%"];
        }
        $lst = Db::name('link')
            ->where($map)
            ->paginate(10);
        $this->assign('lst',$lst);
        return $this->fetch();
    }
    //修改列表的开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('link')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('link')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('link')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加友链
    public function linkAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');

            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }

            $param['create_time'] = time();
            $param['update_time'] = time();
            $res = Db::name('link')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '添加友链成功']);
            }else{
                return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '添加友链失败，请重试']);
            }
        }
        return $this->fetch();
    }
    //编辑
    public function linkEdit()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['update_time'] = time();
            $data = Db::name("link")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'link/index', 'msg' => '修改友链失败']);
            }else{
                return json(['code' => 1, 'url' => 'link/index', 'msg' => '修改友链成功']);
            }
        }
        $link = Db::name('link')->where(array('id'=>$id))->find();
        $this->assign('link',$link);
        return $this->fetch();
    }
    //删除
    public function linkDel()
    {
        $id = input('id');
        $res = Db::name('link')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除友链成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除友链，请重试']);
        }
    }
}